package cn.amebius.simpledocker.service.impl;

import cn.amebius.simpledocker.controller.request.AddDockerClientRequest;
import cn.amebius.simpledocker.controller.request.UpdateDockerClientRequest;
import cn.amebius.simpledocker.entity.po.DockerPo;
import cn.amebius.simpledocker.exception.DataErrorException;
import cn.amebius.simpledocker.mapper.DockerMapper;
import cn.amebius.simpledocker.service.DockerService;
import cn.hutool.core.date.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * Docker服务实现类
 * @author 阿伟
 * @date 2023/7/28 20:23
 */
@Service
public class DockerServiceImpl implements DockerService {

    @Autowired
    DockerMapper dockerMapper;

    @Override
    public List<DockerPo> getAllDocker() {
        List<DockerPo> dockerPos = dockerMapper.selectAll();
        if (dockerPos == null) {
            throw new DataErrorException("数据异常:docker主机丢失");
        }
        return dockerPos;
    }

    @Override
    public DockerPo addDocker(AddDockerClientRequest adcr) {
        if (adcr.getUsername().isEmpty()) {
            adcr.setUsername("空");
        }
        if (adcr.getPassword().isEmpty()) {
            adcr.setPassword("空");
        }
        if (dockerMapper.selectByName(adcr.getName()) != null) {
            throw new DataErrorException("数据异常: docker主机名称已经存在！");
        }
        if (dockerMapper.selectByHostAndPort(adcr.getHost(),adcr.getPort()) != null) {
            throw new DataErrorException("数据异常: docker主机已经存在！");
        }
        try {
            dockerMapper.insertOne(adcr,DateTime.now());
            return dockerMapper.selectByName(adcr.getName());
        } catch (Exception e) {
            throw new DataErrorException("数据异常:docker主机添加失败");
        }
    }

    @Override
    public DockerPo editDocker(UpdateDockerClientRequest udcr) {
        if (udcr.getUsername().isEmpty()) {
            udcr.setUsername("空");
        }
        if (udcr.getPassword().isEmpty()) {
            udcr.setPassword("空");
        }
        DockerPo oldDp = dockerMapper.selectById(udcr.getKey());
        if (!oldDp.getName().equals(udcr.getName())) {
            if (dockerMapper.selectByName(udcr.getName()) != null) {
                throw new DataErrorException("数据异常: docker主机名称已经存在！");
            }
        }
        if (!oldDp.getHost().equals(udcr.getHost()) || !oldDp.getPort().equals(udcr.getPort())) {
            if (dockerMapper.selectByHostAndPort(udcr.getHost(), udcr.getPort()) != null) {
                throw new DataErrorException("数据异常: docker主机已经存在！");
            }
        }
        try {
            dockerMapper.updateById(udcr);
        } catch (Exception e) {
            throw new DataErrorException("数据异常：docker主机修改失败！");
        }
        return dockerMapper.selectById(udcr.getKey());
    }

    @Override
    public void deleteClient(Integer key) {
        try {
            dockerMapper.updateDeletedById(key);
        } catch (Exception e) {
            throw new DataErrorException("数据异常：docker主机删除失败！");
        }
    }

}
